package com.cvpoc.spring.common.controller.solrsearch;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.common.SolrDocumentList;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import com.cvpoc.exception.BusinessException;
import com.cvpoc.service.SolrService;

public class RechercheCollaborateurController extends MultiActionController {

	/**
	 * 
	 * Instantiation de la variable logger destinée à 
	 * recevoir les methodes du gestionnaire de log
	 * 
	 */
    protected final Log logger = LogFactory.getLog(getClass());
    
    /**
     * 
     * Instantiation de la classe contenant les template.s 
     * 
     */
    protected String templatefoldercommon;

    /**
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * 
     */
    public ModelAndView formulaireRecherche(HttpServletRequest request,
    		HttpServletResponse response)
            throws ServletException, IOException {
   
        logger.info("affichage simple formulaire recherche collaborateur.");

        return new ModelAndView(templatefoldercommon + "formulaireRecherche");
    }
    
    /**
     * 
     * Affichage de la liste des collaborateurs obtenu en fonction des parametres
     * en provenance du request.
     * 
     * @param request
     * @param response
     * @return
     * @throws ServletException
     * @throws IOException
     * @throws SolrServerException 
     * @throws BusinessException 
     * 
     */
    public ModelAndView reponseRecherche(HttpServletRequest request,
    		HttpServletResponse response)
            throws ServletException, IOException, BusinessException, SolrServerException {

        logger.info(request.getParameter("recherche"));

        if(request.getParameter("recherche") != null && request.getParameter("recherche").isEmpty() == false) {
    		SolrService solrService = (SolrService) getWebApplicationContext().getBean("solrService");
    		SolrServer solrServer	= solrService.solrServer();
    		
    		SolrQuery solrQuery		= new SolrQuery().
    				setQuery(request.getParameter("recherche")).
    				addSortField( "nom", SolrQuery.ORDER.asc ); 
            
            SolrDocumentList documents 	= solrService.SolrServerDocuments(solrServer, solrQuery);
        	
        	if(documents.getNumFound() > 0) {
				request.setAttribute("collaborateurs", documents);
			} 
			return new ModelAndView(templatefoldercommon + "reponseRecherche");
        } else {
        	return new ModelAndView(templatefoldercommon + "formulaireRecherche");
        }
    }

    /**
     * 
     * Récupération du nom du dossier concernant ce controlleur depuis
     * templatefolder.properties et application de la mutation.
     * 
     * @param templatefoldercommon
     * 
     */
	public void setTemplatefoldercommon(String templatefoldercommon) {
		this.templatefoldercommon = templatefoldercommon;
	}
    
}
